package cn.pugle.selenium;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

/**
 * generate By Selenium IDE <br>
 *
 * @author tzp
 */
public class Td {
    private ChromeDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
//        driver = new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver", "/home/tzp/Downloads/chromedriver");//
        driver = new ChromeDriver();
        baseUrl = "https://www.talkingdata.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testTd() throws Exception {
        login(driver);
        driver.get(baseUrl + "/tracking/summary.html?zh-CN&d28da5a1b458#/channel_list?appkey=d9ff4f15f8c5486aa834fccd2d4b4d02");
        for (int second = 0; ; second++) {
            if (second >= 60) fail("timeout");
            try {
                if (isElementPresent(By.linkText("新建推广活动"))) break;
            } catch (Exception e) {
            }
            Thread.sleep(1000);
        }

        for (int second = 0; ; second++) {
            if (second >= 60) fail("timeout");
            try {
                if (!isElementPresent(By.cssSelector("div.chartTableLoading.facebookG"))) break;
            } catch (Exception e) {
            }
            Thread.sleep(1000);
        }

        driver.findElement(By.linkText("新建推广活动")).click();
        for (int second = 0; ; second++) {
            if (second >= 60) fail("timeout");
            try {
                if (isElementPresent(By.id("channelId"))) break;
            } catch (Exception e) {
            }
            Thread.sleep(1000);
        }

        driver.findElement(By.id("channelId")).clear();
        driver.findElement(By.id("channelId")).sendKeys("校园地推");
        driver.findElement(By.id("name")).clear();
        driver.findElement(By.id("name")).sendKeys("暑期检测98");
//        driver.findElement(By.linkText("保存")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    public static boolean login(ChromeDriver driver) throws InterruptedException {
        driver.get("https://account.talkingdata.com");
        Thread.sleep(2000L);
        WebElement email = driver.findElementByName("email");
        email.click();
        email.sendKeys("chenzhiyuan@soyoung.com");

        WebElement password = driver.findElementByName("password");
        new Actions(driver).moveToElement(password).click().perform();
        new Actions(driver).moveToElement(password).click().perform();
        password.clear();
        password.sendKeys("Sy2016");

        WebElement basic_login = driver.findElementById("basic_login");
        WebElement login = basic_login.findElement(By.className("btn-login"));
        login.click();
        Thread.sleep(2000L);
        System.out.println("cc");
        return true;
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private boolean isAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            String alertText = alert.getText();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
}
